﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CustomsNuke.Engine.CustomsException;
using CustomsNuke.Engine.Data;
using CustomsNuke.Scheduling.Utility;
using Quartz;

namespace CustomsNuke.Scheduling.Jobs
{
    class NotificationLetPassJob : IJob
    {
        CustomsNukeContext _customsNukeContext = new CustomsNukeContext();
        public void Execute(IJobExecutionContext context)
        {
            try
            {
                int taskId = int.Parse(context.JobDetail.Key.Name);
                Task currTask = _customsNukeContext.Task.FirstOrDefault(x => x.Id == taskId && !x.IsDeleted);

                if (currTask == null)
                {
                    Global.Current.Scheduler.DeleteJob(context.JobDetail.Key);
                    return;
                }

                if (currTask.TaskStateId == 5)
                {
                    Global.Current.Scheduler.DeleteJob(context.JobDetail.Key);
                    return;
                }

                ContainerAdmission admission =
                    _customsNukeContext.ContainerAdmission.FirstOrDefault(x => x.TaskId == taskId && !x.IsDeleted);

                var notifying = currTask.TaskNotifying.FirstOrDefault(p => p.IsDeleted == false);

                if (notifying == null)
                {
                    return;
                }

                if (admission != null)
                {
                    StringBuilder sb = new StringBuilder();
                    sb.Append("箱号：" + admission.ContainerNumber);
                    sb.Append("船名：" + admission.Conveyance);
                    sb.Append("航次：" + admission.VoyageNumber);
                    sb.Append("提运单：" + admission.BillNumber);
                    sb.Append("放行状态：" + admission.AdmissionStatus);

                    if (notifying.NotifyingWhenStatusChanged)
                    {
                        if (admission.AdmissionStatus == "已放行")
                        {
                            if (notifying.NotifyingByEmail)
                            {
                                NotificationSendUtils.SendEmail("", sb.ToString());
                            }
                            if (notifying.NotifyingBySMS)
                            {
                                NotificationSendUtils.SendSMS("", sb.ToString());
                            }
                            Global.Current.Scheduler.DeleteJob(context.JobDetail.Key);
                        }
                    }
                    else if (notifying.NotifyingPeriodically)
                    {
                        if (notifying.NotifyingByEmail)
                        {
                            NotificationSendUtils.SendEmail("", sb.ToString());
                        }
                        if (notifying.NotifyingBySMS)
                        {
                            NotificationSendUtils.SendSMS("", sb.ToString());
                        }
                    }
                }
                else
                {
                    //如果是最后一次，发送没有抓到数据信息。
                    if (context.NextFireTimeUtc == null)
                    {
                        if (notifying.NotifyingByEmail)
                        {
                            NotificationSendUtils.SendEmail("", "没有查到箱号为" + currTask.ParameterContainerNumber + "的相关信息。");
                        }
                        if (notifying.NotifyingBySMS)
                        {
                            NotificationSendUtils.SendSMS("", "没有查到箱号为" + currTask.ParameterContainerNumber + "的相关信息。");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                CustomsExceptionManager.HandleException(ex, ExceptionType.NothrowException);
            }
        }
    }
}
